package com.sita.tboard.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sita.bike.R;
import com.sita.bike.persistence.Route;
import com.sita.bike.support.GlobalContext;
import com.sita.tboard.ui.view.SubListView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangde001 on 2015/12/18.
 */
public class RoadMonthAdapter extends BaseAdapter {
    public Context context;
    public ArrayList<String> arrayList_month;
    private List<Route> monthTracks;
    private long timeStart, timeEnd;
    private TextView tv_number;
    private ArrayList<String> arrayList_data = new ArrayList<>();
    RoadDayAdapter roadDataAdapter;

    public RoadMonthAdapter(Context context, ArrayList<String> arrayList_month, List<Route> monthTracks) {
        this.context = context;
        this.arrayList_month = arrayList_month;
        this.monthTracks = monthTracks;
    }

    @Override
    public int getCount() {
        return arrayList_month.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList_month.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        // positon 用来识别是某个月
        // arrayList_month list [position] ==> 是那个月 monthIndex
        ViewHolder viewholder;
        if (convertView == null) {
            viewholder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.roadrocad_expandable, null);
            viewholder.textView = (TextView) convertView.findViewById(R.id.road_recond);
            viewholder.subListView = (SubListView) convertView.findViewById(R.id.road_record_list_day);
            viewholder.tv_number = (TextView) convertView.findViewById(R.id.tv_max_speed);
            convertView.setTag(viewholder);
        } else {
            viewholder = (ViewHolder) convertView.getTag();
        }


        ArrayList<String> arrayListDay = new ArrayList<>();
        for (int i = 0; i < monthTracks.size(); i++) {
            SimpleDateFormat dateFm = new SimpleDateFormat("MM" + "月" + "dd" + "日"); //格式化当前系统日期
            String datestarttime = dateFm.format(monthTracks.get(i).getStartTime());
            arrayListDay.add(datestarttime);
        }

        // // TODO: 2016/1/25
        // arrayListDay = monthTracks.subList(start, end) 根据月份来 获得 sublist


        roadDataAdapter = new RoadDayAdapter(GlobalContext.getGlobalContext(), arrayListDay, monthTracks);
        viewholder.subListView.setAdapter(roadDataAdapter);
        viewholder.textView.setText(arrayList_month.get(position).toString());
        viewholder.tv_number.setText(monthTracks.size() + "次");
        return convertView;
    }

    public void refresh(ArrayList<String> arrayList_month, List<Route> monthTracks) {
        this.arrayList_month = arrayList_month;
        this.monthTracks = monthTracks;
        notifyDataSetChanged();
    }


    class ViewHolder {
        TextView textView, tv_number;
        SubListView subListView;
    }


}
